home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 8614 / 8614.xpi / modules / application / ContextExtractor.jsm next >
Text File  |  2010-02-10  |  5KB  |  207 lines

  1. // DO NOT import this into the global namespace, but instead
  2. // import it into your own namespace wrapper
  3.  
  4. var EXPORTED_SYMBOLS = ["CONTEXT_EXTRACTOR"];
  5.  
  6. Components.utils.import("resource://glydo/utils/prototype_xul_1_6_0_3_modified.jsm");
  7. Components.utils.import("resource://glydo/utils/io.jsm");
  8. Components.utils.import("resource://glydo/utils/Prefs.jsm");
  9. Components.utils.import("resource://glydo/utils/Utils.jsm");
  10. Components.utils.import("resource://glydo/utils/ScriptManager.jsm");
  11. Components.utils.import("resource://glydo/application/extractors/BuiltinExtractors.jsm");
  12.  
  13. const EXTRACTORS_JSON_RESOURCE_NAME="extractors.json";
  14.  
  15. var ContextExtractor = Prototype.Class.create({
  16.     initialize: function(scriptManager) {
  17.         this.scriptManager = scriptManager;
  18.         this.scriptManager.addListener(this);
  19.         this.loadExtractors();
  20.     },
  21.  
  22.     onResourcesUpdated: function(resourceNames) {
  23.         if (resourceNames == null || resourceNames.indexOf(EXTRACTORS_JSON_RESOURCE_NAME) != -1) {
  24.             
  25.             this.loadExtractors();
  26.         }
  27.     },
  28.     
  29.     loadExtractors: function() {
  30.         var json = this.scriptManager.getResource(EXTRACTORS_JSON_RESOURCE_NAME);
  31.         if (!json) {
  32.             this.extractorsParamsByDomain = {};
  33.         } else {
  34.             this.extractorsParamsByDomain = Prototype.S.decodeJSON(json);
  35.         }
  36.  
  37.         
  38.     },
  39.     
  40.     
  41.  
  42.  
  43.     extract: function(doc,options) {
  44.         var extractorsParams = this.getExtractorsParamsForDocument(doc);
  45.         if (!extractorsParams) {
  46.             return null;
  47.         }
  48.         
  49.         var firstEx = this.getAllExtractorForParams(extractorsParams.extractors);
  50.         var secondEx = this.getAllExtractorForParams(extractorsParams.fallbacks);
  51.         var thirdEx = this.getAllExtractorForParams(Utils.arrayify(this.extractorsParamsByDomain["globalFallback"]));
  52.         var ex = this.getFirstExtractorForParams([firstEx,secondEx,thirdEx]);
  53.         
  54.         var task = new ContextExtractor.Task(this.scriptManager,doc,ex,options,false);
  55.         task.execute();
  56.         return task;
  57.     },
  58.  
  59.     getAllExtractorForParams: function(extractors) {
  60.         if (!extractors) {
  61.             return null;
  62.         }
  63.         var e = [];
  64.         var l = extractors.length;
  65.         for (var i = 0; i < l; ++i) {
  66.             if (extractors[i]) {
  67.                 e.push(extractors[i]);
  68.             }
  69.         }
  70.         if (e.length == 0) {
  71.             return null;
  72.         }
  73.         if (e.length == 1) {
  74.             return e[0];
  75.         }
  76.         return ({
  77.             extractor: "com.glydo.extractors.builtins.all",
  78.             options: {
  79.                 candidates: e
  80.             },
  81.         });
  82.     },
  83.  
  84.     getFirstExtractorForParams: function(extractors) {
  85.         if (!extractors) {
  86.             return null;
  87.         }
  88.         var e = [];
  89.         var l = extractors.length;
  90.         for (var i = 0; i < l; ++i) {
  91.             if (extractors[i]) {
  92.                 e.push(extractors[i]);
  93.             }
  94.         }
  95.         if (e.length == 0) {
  96.             return null;
  97.         }
  98.         if (e.length == 1) {
  99.             return e[0];
  100.         }
  101.         return ({
  102.             extractor: "com.glydo.extractors.builtins.first",
  103.             options: {
  104.                 candidates: e,
  105.                 ignoreFailures: true
  106.             }
  107.         });
  108.     },
  109.  
  110.     getExtractorsParamsForDocument: function(doc) {
  111.         var extractors = [];
  112.         var fallbacks = [];
  113.         // Locate candidate extractors
  114.         var url = doc.documentURI;
  115.         var hld = Utils.getHighLevelDomainName(url).toLowerCase();
  116.         var hldExtractors = this.extractorsParamsByDomain[hld];
  117.         if (hldExtractors != undefined) {
  118.             for (var re in hldExtractors) {
  119.                 var match = new RegExp(re).exec(url);
  120.                 if (match != null) {
  121.                     extractors.push.apply(extractors,Utils.arrayify(hldExtractors[re]));
  122.                 }
  123.             }
  124.             fallbacks.push.apply(fallbacks,Utils.arrayify(hldExtractors["fallback"]));
  125.         } else {
  126.             extractors.push.apply(extractors,Utils.arrayify(this.extractorsParamsByDomain["*"]));
  127.         }
  128.         return ({extractors: extractors, fallbacks: fallbacks});
  129.     },
  130.     
  131. });
  132.  
  133. ContextExtractor.Task = Prototype.Class.create({
  134.     initialize: function(scriptManager,doc,extractorParams,options) {
  135.         this.document = doc;
  136.         this.scriptManager = scriptManager;
  137.         this.extractorParams = extractorParams;
  138.         this.options = options;
  139.     },
  140.     
  141.     execute: function() {
  142.         // Find extractor code
  143.         var id;
  144.         var options;
  145.         if (!this.extractorParams) {
  146.             id = null;
  147.             options = null;
  148.         } else if (typeof this.extractorParams == "string") {
  149.             id = this.extractorParams;
  150.             options = null;
  151.         } else {
  152.             id = this.extractorParams["extractor"];
  153.             options = this.extractorParams["options"];
  154.         }
  155.         if (!id) {
  156.             this.taskFailed("No extractor id specified");
  157.             return;
  158.         }
  159.         // Create arguments for extractor
  160.         var callbacks = {};
  161.         if (this.options["onContextItem"]) {
  162.             callbacks["notifyContextItemExtracted"] = this.options["onContextItem"];
  163.         }
  164.         callbacks["notifyTaskDone"] = Prototype.F.bind(this.onTaskDone,this);
  165.         callbacks["notifyTaskFailed"] = Prototype.F.bind(this.onTaskFailed,this);
  166.         
  167.         try {
  168.             this.scriptManager.invoke(this.document,id,options,callbacks,[BuiltinExtractors]);
  169.         } catch (ex) {
  170.             var msg = (typeof ex == "string") ? ex : (ex.name + ": " + ex.message);
  171.             this.onTaskFailed(ex);
  172.             
  173.         }
  174.     },
  175.     
  176.     onTaskDone: function() {
  177.         if (this.options["onCompleted"]) {
  178.             try {
  179.                 this.options["onCompleted"]();
  180.             } catch (ex) {
  181.                 if (Components) {
  182.                     Components.utils.reportError(ex);
  183.                 } else {
  184.                     throw ex;
  185.                 }
  186.             }
  187.         }
  188.     },
  189.  
  190.     onTaskFailed: function(reason) {
  191.         if (this.options["onFailure"]) {
  192.             try {
  193.                 this.options["onFailure"](reason);
  194.             } catch (ex) {
  195.                 if (Components) {
  196.                     Components.utils.reportError(ex);
  197.                 } else {
  198.                     throw ex;
  199.                 }
  200.             }
  201.         }
  202.     },
  203.  
  204. });
  205.  
  206. var CONTEXT_EXTRACTOR = new ContextExtractor(SCRIPT_MANAGER);
  207.